﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class StatisticsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Admin/Statistics/
        public ViewResult Index(int Year = 0, string ClubId = "", int CompetitorId = 0)
        {
            if (Year == 0)
                Year = DateTime.Now.Year;

            var clubs = db.Clubs.Where(c => c.Active == true).OrderBy(c => c.City);
            var competitors = db.Competitors.Where(c => c.ClubId == ClubId & c.Competitor == true).OrderBy(c => c.Surname);

            ViewBag.Year = new SelectList(db.Years, "Year", "Year", Year);
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", ClubId);
            ViewBag.CompetitorId = new SelectList(competitors, "ID", "FullName", CompetitorId);

            var statistics = db.Statistics.Where(s => s.Year == Year & s.CompetitorId == CompetitorId);

            return View(statistics);
        }

        //
        // GET: /Admin/Statistics/Details/5

        public ViewResult Details(int id)
        {
            Statistics statistics = db.Statistics.Single(s => s.ID == id);
            return View(statistics);
        }

        // GET: /Admin/Statistics/Create
        public ActionResult Create(int Year = 0, string ClubId = "", int CompetitorId = 0)
        {
            if (Year == 0)
            {
                Year = DateTime.Now.Date.Year;
            }

            var clubs = db.Clubs.Where(c => c.Active == true).OrderBy(c => c.City).ToList();
            var competitors = db.Competitors.Where(c => c.ClubId == ClubId & c.Competitor == true).OrderBy(c => c.Surname).ThenBy(c => c.Firstname).ToList();

            var disciplines = db.Disciplines.Where(d => d.Active == true);
            var statCompetitions = db.StatisticsCompetitions.Where(c => c.Date.Value.Year == Year).ToList();

            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", ClubId);
            ViewBag.CompetitorId = new SelectList(competitors, "ID", "FullName", CompetitorId);
            
            ViewBag.DisciplineId = new SelectList(disciplines, "ID", "Name");

            ViewBag.Year = new SelectList(db.Years, "Year", "Year", Year);
            ViewBag.CompetitionId = new SelectList(statCompetitions, "ID", "City");

            return View();
        } 

        // POST: /Admin/Statistics/Create
        [HttpPost]
        public ActionResult Create(Statistics statistics)
        {
            if (ModelState.IsValid)
            {
                db.Statistics.AddObject(statistics);
                db.SaveChanges();
                return RedirectToAction("Index", new { Year = statistics.Year, ClubId = statistics.Competitors.ClubId, CompetitorId = statistics.CompetitorId  });  
            }

            statistics.Competitors = db.Competitors.SingleOrDefault(c => c.ID == statistics.CompetitorId);

            var clubs = db.Clubs.Where(c => c.Active == true).OrderBy(c => c.City).ToList();
            var competitors = db.Competitors.Where(c => c.ClubId == statistics.Competitors.ClubId & c.Competitor == true).OrderBy(c => c.Surname).ThenBy(c => c.Firstname).ToList();

            var disciplines = db.Disciplines.Where(d => d.Active == true);
            var statCompetitions = db.StatisticsCompetitions.Where(c => c.Date.Value.Year == statistics.Year).ToList();

            ViewBag.ClubId = new SelectList(clubs, "ID", "Name");
            ViewBag.CompetitorId = new SelectList(competitors, "ID", "FullName");

            ViewBag.DisciplineId = new SelectList(disciplines, "ID", "Name");

            ViewBag.Year = new SelectList(db.Years, "Year", "Year", statistics.Year);
            ViewBag.CompetitionId = new SelectList(statCompetitions, "ID", "City");
            
            return View(statistics);
        }
        
        // GET: /Admin/Statistics/Edit/5
        public ActionResult Edit(int id)
        {
            Statistics statistics = db.Statistics.Single(s => s.ID == id);

            if (statistics == null)
            {
                return HttpNotFound();
            }
            
            return View(statistics);
        }

        // POST: /Admin/Statistics/Edit/5
        [HttpPost]
        public ActionResult Edit(Statistics statistics)
        {
            if (ModelState.IsValid)
            {
                db.Statistics.Attach(statistics);
                db.ObjectStateManager.ChangeObjectState(statistics, EntityState.Modified);
                db.SaveChanges();

                return RedirectToAction("Index", new { Years = statistics.Year, ClubId = statistics.Competitors.ClubId, CompetitorId = statistics.CompetitorId });
            }

            statistics.Competitors = db.Competitors.SingleOrDefault(c => c.ID == statistics.CompetitorId);
            return View(statistics);
        }

        // GET: /Admin/Statistics/Delete/5
        public ActionResult Delete(int id)
        {
            Statistics statistics = db.Statistics.Single(s => s.ID == id);
            return View(statistics);
        }

        // POST: /Admin/Statistics/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Statistics statistics = db.Statistics.Single(s => s.ID == id);

            int myYear = Convert.ToInt32(statistics.Year);
            string myClubId = db.Competitors.SingleOrDefault(c => c.ID == statistics.CompetitorId).ClubId;
            int myCompetitorId = Convert.ToInt32(statistics.CompetitorId);

            db.Statistics.DeleteObject(statistics);
            db.SaveChanges();
            return RedirectToAction("Index", new { Years = myYear, ClubId = myClubId, CompetitorId = myCompetitorId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #region Cascade Dropdown

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadCompetitorsForClub(string id)
        {
            var competitorsList = db.Competitors.Where(c => c.ClubId == id & c.Competitor == true).OrderBy(c => c.Surname).ToList();
            SelectList comboData = new SelectList(competitorsList, "ID", "FullName");
            return Json(comboData, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadStatCompForYear(string id)
        {
            int selectedYear = Convert.ToInt32(id);
            var statisticsCompetitions = db.StatisticsCompetitions.Where(sc => sc.Date.Value.Year == selectedYear).OrderBy(sc => sc.Date).ToList();
            SelectList comboData = new SelectList(statisticsCompetitions, "ID", "City");
            return Json(comboData, JsonRequestBehavior.AllowGet);
        }
               
        #endregion


    }
}